/**
 * Created by Kpeng on 2017/7/3.
 * Depending on the jquery-1.12.4.min.js.
 * Depending on the common.js.
 * Depending on the style.min.css.
 * To cite this document, always state the source as shown above.
 * -------------------------------
 * Module:服务管理
 * -------------------------------
 */

$(function () {
  wzERP.Staff.init();
});

wzERP.Staff.init = function () {
  wzERP.Common.inlineDate('#rangestart', '#rangeend');
  wzERP.ServiceClass.addHandle(); //添加员工
  wzERP.ServiceClass.editHandle(); //编辑员工
  wzERP.ServiceClass.delHandle(); //删除员工
  wzERP.ServiceClass.turnPage();//分页
  wzERP.ServiceClass.isActive();//是否显示
  //wzERP.Staff.selectDepart();
};

//添加
wzERP.ServiceClass.addHandle = function () {
  var opt = {
    ele: '.addBtn',
    modalBox: '#modal',
    modalTitleStr: '添加分类',
    onApprove: function () {
      var name = $("#serviceClass").find("[name='serviceCode']").val();
      var flag = wzERP.Common.isExistTypeHandle("erp_service_classification",
          " service_code='" + name + "'");
      if (flag) {
        layer.msg('分类编号已存在', {icon: 2});
        return false;
      }
      wzERP.Common.saveHandle('#serviceClass', '#submitServiceUrl',
          $(this).find('.setId'), wzERP.Common.delayReload(wzERP.delayTime));
    },
    snippets: this.url,
    callback: function () {
      this.type = this.event.data('type');
      if (this.type !== 'add') {
        var getIds = $('#modal').find('.setId').data('id');
        this.url = $("#addServiceHtml").val() + "?id=" + getIds + "&type="
            + this.type;
      } else {
        this.url = $("#addServiceHtml").val()
      }
    }
  };
  wzERP.Common.handleData(opt);
};

//编辑
wzERP.ServiceClass.editHandle = function () {
  var opt = {
    ele: '.editBtn',
    modalBox: '#modal',
    modalTitleStr: '编辑分类信息',
    onApprove: function () {
      var org = $("#serviceClass").find("[name='serviceCode']")[0].defaultValue;
      var name = $("#serviceClass").find("[name='serviceCode']").val();
      if (org != name) {
        var flag = wzERP.Common.isExistTypeHandle("erp_service_classification",
            " service_code='" + name + "'");
        if (flag) {
          layer.msg('分类编号已存在', {icon: 2});
          return false;
        }
      }
      wzERP.Common.saveHandle('#serviceClass', '#updateServiceUrl',
          $(this).find('.setId'), wzERP.Common.delayReload(wzERP.delayTime));
    },
    snippets: $("#serviceUpdateHtml").val()
  };
  wzERP.Common.handleData(opt);
};

//删除
wzERP.ServiceClass.delHandle = function () {
  /*var opt = {
   ele: '.delBtn',
   modalBox: '#modal',
   modalTitleStr: '',
   onApprove: function () {
   wzERP.Common.deleteData('#delServiceUrl', $(this).find('.setId'));
   },
   snippets: $("#serviceDeleteHtml").val()
   };
   wzERP.Common.handleData(opt);*/
  $('#main').on('click', '.delBtn', function () {
    var $self = $(this);
    layer.alert('确定删除该分类吗？', {
          title: '提示信息',
          btn: ['取消', '确认'],
          btn1: function (index) {
            layer.close(index)
          },
          btn2: function () {
            wzERP.Common.deleteData('#delServiceUrl', $self)
          }
        }
    );
  })
};

//分页跳转
wzERP.ServiceClass.turnPage = function () {
  var opt = {
    pageUrl: $("#servicePageUrl").val(),
    pageSearch: "#searchForm",
    pagesTotal: parseInt($('#pages').val()),
    pageCurr: parseInt($('#current').val()),
    pageSize: parseInt($('#pageSize').val()),
    currId: "#current"
  };
  wzERP.Common.pagingHandle(opt);
};

//
function search() {
  $("#current").val(1);
  $("#searchForm").action = "/basedata/erpServiceClass/queryPage";
  $("#searchForm").submit();
}

//显示状态
wzERP.ServiceClass.isActive = function () {
  var actives = $('#servicelist').find('.toggle').find('input');
  actives.on('click', function () {
    var opt = {
      ele: $(this),
    };
    wzERP.ServiceClass.slideCheck(opt);
  });
};

//滑块开关
wzERP.ServiceClass.slideCheck = function (_option) {
  /*Arg Guide
   @ opt.ele:触发元素
   @ opt.chekcedFn: 打开时true自调
   @ opt.uncheckedFn:关闭时false自调
   */
  _option.id = $(_option.ele).parent().data('id') || "";
  _option.active = $(_option.ele).prop('checked');
  var changeAcitveUrl = $('#changeShowUrl').val(),
      postData = {id: _option.id, isView: _option.active};
  (function () {
    $.post(changeAcitveUrl, postData, function (data) {
      switch (data.status) {
        case 0:
          layer.msg(data.msg, {icon: 2});  //成功
          break;
        case 1:
          layer.msg(data.msg, {icon: 5}); //失败
          wzERP.Common.delayReload(500);
          break;
        default:
          layer.msg(data.msg, {icon: 7});  //默认
      }
    })
  })();
};

//表单验证器
wzERP.ServiceClass.verifier = function (_selector) {
  /*Arg Guide
   @_selector: 对象选择器
   */
  $(_selector).form({
    on: 'blur',
    // inline: true,
    transition: 'fade',
    fields: {
      //分类名称
      classificationName: {
        identifier: 'classificationName',
        rules: [{
          type: 'empty',
          prompt: '请输分类名称'
        },
          {
            type: 'regExp[/^[^\\s]{1,20}$/]',
            prompt: '请输分类名称'
          }
        ]
      },
      //编号
      serviceCode: {
        identifier: 'serviceCode',
        rules: [{
          type: 'empty',
          prompt: '请输编号'
        }, {
          type: 'regExp[/^[A-Z0-9]{2,2}$/]',
          prompt: '两位大写字母或数字，不可与其他重复'
        }
        ]
      },
      //上级分类
      parentId: {
        identifier: 'parentId',
        rules: [{
          type: 'empty',
          prompt: '请输入姓名'
        }]
      }
    }
  });

};


